/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mysweethome.jsfmanagedbean;


import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import org.primefaces.event.FileUploadEvent;

/**
 *
 * @author Welcome
 */
@ManagedBean(name = "fileUploadController")
@SessionScoped
public class FileUploadController {
//    private String destination="C:\\tmp\\";
//
//    public void upload(FileUploadEvent event) {  
//        FacesMessage msg = new FacesMessage("Success! ", event.getFile().getFileName() + " is uploaded.");  
//        FacesContext.getCurrentInstance().addMessage(null, msg);
//                
//        try {
//            copyFile(event.getFile().getFileName(), event.getFile().getInputstream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//    }  
//
//    public void copyFile(String fileName, InputStream in) {
//           try {
//
//                // write the inputStream to a FileOutputStream
//                OutputStream out = new FileOutputStream(new File(destination + fileName));
//             
//                int read = 0;
//                byte[] bytes = new byte[1024];
//             
//                while ((read = in.read(bytes)) != -1) {
//                    out.write(bytes, 0, read);
//                }
//             
//                in.close();
//                out.flush();
//                out.close();
//             
//                System.out.println("New file created!");
//                } catch (IOException e) {
//                System.out.println(e.getMessage());
//                }
//    }
    
    public FileUploadController(){
        
    }
    
    public void handleFileUpload(FileUploadEvent event) {  
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
}
